package ext.tianma.part.RawMaterial.datautility;

import java.util.ArrayList;

import wt.util.WTException;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.dataUtilities.AttributeDataUtilityHelper;
import com.ptc.core.components.factory.dataUtilities.DefaultDataUtility;
import com.ptc.core.components.rendering.GuiComponent;
import com.ptc.core.components.rendering.guicomponents.GUIComponentArray;
import com.ptc.core.components.rendering.guicomponents.TextBox;
import com.ptc.core.ui.resources.ComponentMode;
import com.ptc.netmarkets.util.beans.NmCommandBean;


/**
 * 类名: ReadOnlyTextUtility </br>
 * 包名: ext.tianma.doc.SendOut.datautilities
 * 描述: 只读的文本框
 * @author xucr
 * 创建时间: 2021年1月15日
 * 发布版本: V1.0
 */
public class ReadOnlyTextUtility extends DefaultDataUtility{

	@Override
	public Object getDataValue(String component_id, Object datum, ModelContext mc) throws WTException {
		NmCommandBean commandBean = mc.getNmCommandBean();
        Object component = null;
        ComponentMode componentMode = mc.getDescriptorMode();
        component = createPushButtonAndTextBox(component_id, datum, mc, componentMode);
        return component;
	}
	
    private Object createPushButtonAndTextBox(String component_id, Object datum, ModelContext mc, ComponentMode componentMode) throws WTException {
		ArrayList<GuiComponent> components = new ArrayList<GuiComponent>();
		/***** TEXT BOX *****/
		TextBox textBox = createTextBox(component_id, componentMode);
		textBox.setId(component_id);
		textBox.setEnabled(true);
		textBox.setEditable(true);
		textBox.setReadOnly(true);
		textBox.setColumnName(AttributeDataUtilityHelper.getColumnName(component_id, datum, mc));
	    String manufacturer = (String) mc.getRawValue();
	    textBox.setValue(manufacturer);
		components.add(textBox);
		GUIComponentArray gui = new GUIComponentArray(components);
		gui.setRequired(false);
		return gui;
	}
    
    private TextBox createTextBox(String component_id, ComponentMode componentMode) throws WTException {
    	TextBox text = new TextBox();
		text.setMaxLength(200);
		text.setWidth(60);
		text.setId(component_id);
		text.setEditable(isComponentEditable(component_id, componentMode));
		return text;
	}
	
	private boolean isComponentEditable(String component_id, ComponentMode componentMode) throws WTException {
		boolean isReadOnly = componentMode.equals(ComponentMode.EDIT);
		return !(isReadOnly);
	}
}
